package com.mlizhi.base;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

//import org.apache.http.HttpEntity;
//import org.apache.http.util.EntityUtils;

//import android.os.SystemClock;
import android.text.TextUtils;

import com.mlizhi.base.codec.binary.Base64;
import com.mlizhi.base.codec.digest.DigestUtils;

/**
 * This class provides some security utility method.
 * 
 * @author TroyLi
 */
public class SecurityUtil {

	private static final byte[] SECRET_KEY_NORMAL;
	private static final byte[] SECRET_KEY_HTTP;
	private static final byte[] SECRET_KEY_HTTP_CHARGE;
	public static final byte[] SECRET_KEY_HTTP_CHARGE_MLIZHI;
	public final static String SECRET_KEY_HTTP_PARAM = "MLZ2ADMIN Co.Ltd";

	// public static final String KEY_HTTP_CHARGE_ALIPAY = "h9sEVED84X81u9ev";
	//
	// static {
	// SECRET_KEY_NORMAL =
	// DigestUtils.md5(DigestUtils.md5("7U727ALEWH8".getBytes()));
	// SECRET_KEY_HTTP = "sdk_mappn_201008".getBytes();
	// SECRET_KEY_HTTP_CHARGE = "MAPPN-ANDY-XIAN-".getBytes();
	// SECRET_KEY_HTTP_CHARGE_ALIPAY = KEY_HTTP_CHARGE_ALIPAY.getBytes();
	// }
	public static final String KEY_HTTP_CHARGE_MLIZHI = "h9sEVED84X81u9ev";

	static {
		SECRET_KEY_NORMAL = DigestUtils.md5(DigestUtils.md5("7U727ALEWH8"
				.getBytes()));
		SECRET_KEY_HTTP = "sdk_mapmlz_201503".getBytes();
		SECRET_KEY_HTTP_CHARGE = "MAPMLZ-ANDY-XIAN-".getBytes();
		SECRET_KEY_HTTP_CHARGE_MLIZHI = KEY_HTTP_CHARGE_MLIZHI.getBytes();
	}

	public static String decrypt(String encValue) {
		if (TextUtils.isEmpty(encValue))
			return "";

		byte[] bytes = Base64.decodeBase64(Utils.getUTF8Bytes(encValue));
		if (bytes == null)
			return "";

		bytes = new Crypter().decrypt(bytes, SECRET_KEY_NORMAL);
		if (bytes == null)
			return "";

		return Utils.getUTF8String(bytes);
	}

	public static String encrypt(String value) {
		if (value == null)
			return null;

		byte[] bytes = Utils.getUTF8Bytes(value);
		bytes = new Crypter().encrypt(bytes, SECRET_KEY_NORMAL);
		bytes = Base64.encodeBase64(bytes);

		return Utils.getUTF8String(bytes);
	}

	public static String encryptPassword(String targetText, String publicKey) {
		byte[] key = DigestUtils.md5(Utils.getUTF8Bytes(publicKey));
		swapBytes(key);
		reverseBits(key);
		byte[] enc = new Crypter().encrypt(Utils.getUTF8Bytes(targetText), key);
		return Utils.getUTF8String(Base64.encodeBase64(enc));
	}

	public static byte[] encryptHttpBody(String body) {
		return Base64.encodeBase64(new Crypter().encrypt(
				Utils.getUTF8Bytes(body), SECRET_KEY_HTTP));
	}

	public static byte[] encryptHttpChargeBody(String body) {
		return new Crypter().encrypt(Utils.getUTF8Bytes(body),
				SECRET_KEY_HTTP_CHARGE);
	}

	public static byte[] encryptHttpChargePalipayBody(String body) {
		return Base64.encodeBase64(new Crypter().encrypt(
				Utils.getUTF8Bytes(body), SECRET_KEY_HTTP_CHARGE_MLIZHI));
	}


	private static void swapBytes(byte[] b) {
		for (int i = 0; i < b.length; i += 2) {
			byte tmp = b[i];
			b[i] = b[i + 1];
			b[i + 1] = tmp;
		}
	}

	private static void reverseBits(byte[] b) {
		for (int i = 0; i < b.length; i++)
			b[i] ^= 0xFF;
	}

	public static String getTimestamp() {
		String timestamp = String
				.valueOf((int) (System.currentTimeMillis() / 1000));
		return timestamp;
	}

	public static String getMd5String(String timestamp) {

		String checkKey = timestamp + SECRET_KEY_HTTP_PARAM;
		String key = null;
		// key = DigestUtils.md5Hex(checkKey);
		try {
			key = md5(checkKey);
		} catch (NoSuchAlgorithmException e) {
			// e.printStackTrace();
			Utils.E("encrypt error!!!", e);
		}
		return key;
	}

	private final static String md5(String str) throws NoSuchAlgorithmException {
		final char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
				'9', 'a', 'b', 'c', 'd', 'e', 'f' };
		byte[] btInput = str.getBytes();
		// 获得MD5摘要算法的 MessageDigest 对象
		MessageDigest md5Inst = MessageDigest.getInstance("MD5");
		// 使用指定的字节更新摘要
		md5Inst.update(btInput);
		// 获得密文
		byte[] bytes = md5Inst.digest();

		StringBuffer strResult = new StringBuffer();
		// 把密文转换成十六进制的字符串形式
		for (int i = 0; i < bytes.length; i++) {
			strResult.append(hexDigits[(bytes[i] >> 4) & 0x0f]);
			strResult.append(hexDigits[bytes[i] & 0x0f]);
		}
		return strResult.toString();
	}
}
